<?php
/**
 * Created by : PhpStorm
 * User: Shane
 * Date: 2023-03-07
 * Time: 15:42
 */
namespace app\admin\controller;

use app\admin\model\Admin as MAdmin;
use app\admin\model\Role;
use app\AdminBaseController;
use control\EncryptionMethod;


class Admin extends AdminBaseController
{

    /**
     * 后台用户列表
     */
    public function index()
    {
        $adminModel = new MAdmin();
        $res = $adminModel->getList("admin",$this->page, $this->size, array_merge($adminModel->search(),[["id",">","1"]]));
        $assignData=[
            "res"        => $res['data'],
            "search"     => $this->request->get(),
            "query"      => $this->query,
            "count"      => $res['count'],
            "pageTotal"  => $this->size,
            "curr"       => $this->page,
        ];
        return view('index',$assignData);
    }
    /**
     * 添加后台用户
     */
    public function add()
    {
        $role = new Role();
        if($this->request->post()){
            $adminModel = new MAdmin();
            $data=$adminModel->validate();
            $data["createdate"]=time();
            $data["password"]=EncryptionMethod::ts_sha1(base64_encode(sha1(trim($this->data["password"]),true)));
            $data["userimg"]='upload\20211009\e733c95d9d8bac5ca9c1886636cac796.png';
            $this->dbSave("admin",$data);
        }
        return view('add',["r"=>json_encode($role->GetRoleLists())]);
    }
    /**
     * 修改
     */
    public function update(){
        $role = new Role();
        $adminModel = new MAdmin();
        if($this->request->post()){
            $data=$adminModel->validate();
            if($this->data["password"]){
                $data["password"]=EncryptionMethod::ts_sha1(base64_encode(sha1(trim($this->data["password"]),true)));
            }
            $this->dbUpdate("admin",$data,["id"=>$data["id"]]);
        }
        $admin = $adminModel->where('id',$this->data['id'])->find();
        return view('update',["admin"=>$admin,"r"=>json_encode($role->GetRoleLists())]);
    }
    /**
     * 更改状态 启用禁用
     */
    public function status(){
        $adminModel = new MAdmin();
        $data=$adminModel->validate();
        $this->dbUpdate("admin",$data,["id"=>$data["id"]]);
    }
    /**
     * 批量删除 单个删除
     */
    public function delete(){
        $adminModel = new MAdmin();
        $data=$adminModel->validate();
        $where[] = ['id', 'in', explode(',', $data['id'])];
        $this->dbDelete("admin", $where);
    }

}